<?php
namespace Home\Controller;

use Think\Controller;

class LottoController extends Controller
{
    /*
     * 是一个二维数组，记录了所有本次抽奖的奖项信息，
     * 其中id表示中奖等级，prize表示奖品，v表示中奖概率。
     * 注意其中的v必须为整数，你可以将对应的 奖项的v设置成0，即意味着该奖项抽中的几率是0，
     * 数组中v的总和（基数），基数越大越能体现概率的准确性。
     * 本例中v的总和为100，那么平板电脑对应的 中奖概率就是1%，
     * 如果v的总和是10000，那中奖概率就是万分之一了。

               */
    //测试概率
    private function Lotto($group)
    {
        $lotto1 = array(8,9,10,12,1,6,11);
        $lotto2 = array(8,9,10,12,1,2,5);
        $lotto3 = array(8,9,10,12,3,4,7);
        //定义角度
        $angle1=array(
              '0' => array('pid'=>8,'min'=>102,'max'=>122), //售后旗舰店现金抵用券 30元
              '1' => array('pid'=>9,'min'=>247,'max'=>268),//售后旗舰店现金抵用券 50元
              '2' => array('pid'=>10,'min'=>326,'max'=>358),//售后旗舰店现金抵用券 100元
              '3' => array('pid'=>11,'min'=>292,'max'=>309),//车载充电器&amp;破窗器二合一
              '4' => array('pid'=>1,'min'=>55,'max'=>75),//i7
              '5' => array('pid'=>6,'min'=>146,'max'=>166),//星巴克星享卡
              '6' => array('pid'=>12,'min'=>array(10,192), 'max'=>array(40,212))
        );
        $angle2=array(
              '0' => array('pid'=>8,'min'=>102,'max'=>122), //售后旗舰店现金抵用券 30元
              '1' => array('pid'=>9,'min'=>247,'max'=>268),//售后旗舰店现金抵用券 50元
              '2' => array('pid'=>10,'min'=>326,'max'=>358),//售后旗舰店现金抵用券 100元
              '3' => array('pid'=>5,'min'=>292,'max'=>309),//乐扣
              '4' => array('pid'=>1,'min'=>55,'max'=>75),//i7
              '5' => array('pid'=>2,'min'=>146,'max'=>166),//拍立得
              '6' => array('pid'=>12,'min'=>array(23,194), 'max'=>array(40,212))
        );
        $angle3=array(
              '0' => array('pid'=>8,'min'=>102,'max'=>122), //售后旗舰店现金抵用券 30元
              '1' => array('pid'=>9,'min'=>247,'max'=>268),//售后旗舰店现金抵用券 50元
              '2' => array('pid'=>10,'min'=>326,'max'=>358),//售后旗舰店现金抵用券 100元
              '3' => array('pid'=>3,'min'=>55,'max'=>75),//牙刷
              '4' => array('pid'=>4,'min'=>292,'max'=>309),//膳魔师
              '5' => array('pid'=>7,'min'=>146,'max'=>166),//爱奇艺
              '6' => array('pid'=>12,'min'=>array(23,194), 'max'=>array(40,212))
        );
        $angle="";
        if($group==1){
            $lotto = $lotto1;
            $angle=$angle1;
        }
        if($group==2){
            $lotto = $lotto2;
            $angle=$angle2;
        }
        if($group==3){
            $lotto = $lotto3;
            $angle=$angle3;
        }

        // 初始化奖项数组
        \Think\Log::record('初始化奖项数组---Start', 'INFO');
        $Lotto = M('lotto', 'ny_prize_', 'newyear');
        $LottoList = $Lotto->cache(true, 60)->select();
        $i = 0;
        foreach ($LottoList as $key => $value) {
            if (in_array($value['id'], $lotto)) {
                $prize_arr[$i]['pid'] = $value['id'];//奖品id
                $prize_arr[$i]['prize'] = $value['pname'];//奖品名称
                $prize_arr[$i]['v'] = $value['v'];//奖品中奖概率
                $i++;
            }
        }
        \Think\Log::write('组合奖品：'.json_encode($prize_arr), 'INFO');
        \Think\Log::record('初始化奖项数组---end   一共初始化' . count($LottoList) . '个奖品', 'INFO');
        \Think\Log::record('初始化中奖概率数组---Start', 'INFO');
        foreach ($prize_arr as $key => $val) {
            $arr[$val['pid']] = $val['v'];
        }
        \Think\Log::record('中奖概率数组个数' . count($arr), 'INFO');
        \Think\Log::record('初始化中奖概率数组---End', 'INFO');
        $rid = get_rand2($arr); //根据概率获取奖项id

        foreach($angle as $k=>$val){
            if($val['pid']==$rid){
                $result['min'] = $val['min'];
                $result['max'] = $val['max'];
                if($val['pid']==12){
                    $rand=  mt_rand(0,1);
                    $result['min'] =  $result['min'][$rand];
                    $result['max']= $result['max'][$rand];
                }
            }
        }
        $res['angle'] = mt_rand($result['min'],$result['max']); //随机生成一个角度
        $res['PNAME'] = get_pname($prize_arr, $rid); //获取中奖名称
        $res['PID'] = $rid;
        \Think\Log::record('获取中奖pid：' . $rid . '--中奖奖品名称：' . $res['PNAME'], 'INFO');
        return $res;
    }

    /*
     *
     * 抽奖操作
     *
     */

    public function  Do_lotto_options()
    {

        $group = I('get.group');
        //防止漏洞
        $Gtp = "1,2,3";
        $Gtp_arr = explode(',',$Gtp);
        if(!in_array($group,$Gtp_arr)){
            $msg['msg'] = '非法操作！';
            $this->ajaxReturn($msg);
            exit();
        }
        \Think\Log::write('开始抽奖--获取前台转盘版本：'.$group, 'INFO');
        if (strtotime(date('Y-m-d H:i:s')) >= strtotime('2017-2-19 12:59:59')) {
            $msg['status'] = 500;
            $msg['msg'] = '活动已经结束哦！';
            $this->ajaxReturn($msg);
            exit();
           }
        if (checkIsLogin()) {
            $msg['status'] = 500;
            $msg['msg'] = '您尚未登陆！';
            $this->ajaxReturn($msg);
            exit();
        }
        // 判断 是否足够 积分 来抽奖
        $user_point = GetUserSocreByUid(cookie("uid"));
        if ($user_point < 20) {
            $msg['status'] = 500;
            $msg['msg'] = '您的积分不足，无法抽奖！';
            $this->ajaxReturn($msg);
            exit();
        }
        //执行抽奖
        //扣除用户的积分
        Deduction_User_points(20);

        $result = $this->Lotto($group);
        //检查今天的该奖品发放个数   1 为兑换的类型  2 为抽奖的类型
        $res = GetTodayPrizeHave($result['PID'], 2);
        $lotto = GetLottoInfoByPid($result['PID']);

        if ($lotto['id'] == 12) { //未中奖
            Add_Log_ecrm_pointDetil(168, 20);
            $msg['score'] = $user_point-20;
            $msg['status'] = 506;
            $msg['angle']=$result['angle'];
            $msg['PNAME'] = '谢谢参与！';
            $this->ajaxReturn($msg);
            exit();
        }
        if (count($res) >= $lotto['numperday']) {
            Add_Log_ecrm_pointDetil(168, 20);
            $msg['score'] = $user_point-20;
            $msg['status'] = 507;
            $msg['angle']= GetThanksAngle();
            $msg['PNAME'] = '谢谢参与！';
            $this->ajaxReturn($msg);
            exit();
        }
        if ($lotto['left'] <= 0) {
            Add_Log_ecrm_pointDetil(168, 20);
            $msg['score'] = $user_point-20;
            $msg['PNAME'] ='谢谢参与！';
            $msg['status'] = 505;
            $msg['angle']= GetThanksAngle();
            $this->ajaxReturn($msg);
            exit();
        }
        if (CheckIsDuiByPid($result['PID'], 2)) {
            Add_Log_ecrm_pointDetil(168, 20);
            $msg['score'] = $user_point-20;
            $msg['PNAME'] ='谢谢参与！';
            $msg['status'] = 510;
            $msg['angle']= GetThanksAngle();
            $this->ajaxReturn($msg);
            exit();
        }

        //判断是否是卡券类 兑换码 的奖品
        if($lotto['is_code_prize']==1){
            //根据pid 去兑换码库里找一个码
            $code=GetCodeByPid();
            if($code==null || $code==""){
                Add_Log_ecrm_pointDetil(168, 20);
                $msg['score'] = $user_point-20;
                $msg['status'] = 507;
                $msg['angle']= GetThanksAngle();
                $msg['PNAME'] = '谢谢参与！';
                $this->ajaxReturn($msg);
                exit();
            }

        }
        if($lotto['is_url_prize']==1){//判断是否是url类的兑换奖品
            //根据pid 去兑换码库里找一个url码
            $url_code=GetCodeUrlByPid();
            if($url_code==null || $url_code==""){
                Add_Log_ecrm_pointDetil(168, 20);
                $msg['score'] = $user_point-20;
                $msg['status'] = 507;
                $msg['angle']= GetThanksAngle();
                $msg['PNAME'] = '谢谢参与！';
                $this->ajaxReturn($msg);
                exit();
            }
        }
        //判断是否是现金兑换券
        //获取所有的现金数值
        $money = "30,50,100,150";
        $money_arr = explode(',',$money);
        if(in_array($lotto['is_money_prize'],$money_arr)){
            $money_code = GetMoneyCodeByNum($lotto['is_money_prize']);
            if($money_code==null || $money_code==""){
                Add_Log_ecrm_pointDetil(168, 20);
                $msg['score'] = $user_point-20;
                $msg['status'] = 507;
                $msg['angle']= GetThanksAngle();
                $msg['PNAME'] = '谢谢参与！';
                $this->ajaxReturn($msg);
                exit();
            }
        }

        $res = pushNumLottoByPid($lotto['id'], $lotto['left']);
        if ($res==0 || $res==false) {
            Add_Log_ecrm_pointDetil(168, 20);
            $msg['score'] = $user_point-20;
            $msg['status'] = 507;
            $msg['angle']= GetThanksAngle();
            $msg['PNAME'] = '谢谢参与！';
            $this->ajaxReturn($msg);
            exit();
        }
        //减少奖品数量1
        if($res==1){//扣减奖品数量成功 将$code 或者 $url_code 状态改为 1

            if($code['prize_code']!=null || $code['prize_code']!=""){
                changeStatusByCode($code['id']);
            }
            if($url_code['prize_url']!=null || $url_code['prize_url']!=""){
                changeStatusByurl_code($url_code['id']);
            }
            if($money_code['money_code']!=null || $money_code['money_code']!=""){
                changeStatusBymoney_code($money_code['id']);
            }
        }
        //记录 积分项目 到ecrm 的积分项目    还要修改等上线
        Add_Log_ecrm_pointDetil($lotto['item_id'], 20);
        //记录中奖用户  1 为兑换的类型  2 为抽奖的类型
        Add_log_user_prize($result['PID'],2,$code['prize_code'],$url_code['prize_url'],$money_code['money_code']);
        $msg['score'] = $user_point-20;
        $msg['PNAME'] = $result['PNAME'];
        $msg['status'] = 200;
        $msg['angle']= $result['angle'];
        $this->ajaxReturn($msg);
        exit();
    }

}